import {create} from 'zustand';
import {login} from "../services/user";
import {createJSONStorage, persist} from "zustand/middleware";
import {setToken} from '../utils/auth';

export const useGlobalStore = create((set, get) => ({
    user: null,
    isLoggedIn: false,
    loginAsync: async (params, onSuccess) => {
        try {
            const user = await login(params); // 1. 执行登录API调用
            set({ isAuthenticated: true, user }); // 2. 更新状态
            onSuccess?.(() => console.log('成功了', user)); // 3. 如果提供了回调则执行
        } catch (error) {
            throw error; // 抛出错误让组件可以捕获
        }
    },
    logout: async () => set({ isAuthenticated: false, user: null }),
}))
